<?php

ini_set("display_errors",true);
error_reporting(E_ALL ^ E_NOTICE);

include dirname(__FILE__) . '/mysecurimage.php';

// generate float random number
function random_float($min,$max) 
{
   return ($min+lcg_value()*(abs($max-$min)));
}


?>

<html>
	<head>
	</head>
	
	<body>
		<p>	<?php
				echo "<br/>Generate training data...<br/>";
				
				$WIDTH 	= 230;
				$HEIGHT = 80;
				$DATA_DIR = dirname(__FILE__) . "/training_data"; // 70%
				$DATA_DIR_TEST = dirname(__FILE__) . "/test_data"; // 30%				
				$LENGTH = 1;
				$TRAINING_SIZE = 3; // 30% 

				// create training & test folders
				if(!file_exists($DATA_DIR))
				{
						mkdir($DATA_DIR);
				}

				// create dir to store test data
				if(!file_exists($DATA_DIR_TEST))
				{
					mkdir($DATA_DIR_TEST);
				}
				
				
				// === BEGIN generate digit training examples ===
				
				$trainingLetters = "1234567890";
				
				$numChars = strlen($trainingLetters);
				for($x=0; $x<$numChars; $x++)	
				{
					$idx = 0;
					
					$char = $trainingLetters{$x};

					$dirName = $char;
					
					// create dir to store training examples
					if(!file_exists($DATA_DIR . '/' . $dirName))
					{
						mkdir($DATA_DIR . '/' . $dirName);
					}

					// create dir to store test data
					if(!file_exists($DATA_DIR_TEST . '/' . $dirName))
					{
						mkdir($DATA_DIR_TEST . '/' . $dirName);
					}


					// === BEGIN generate examples with distortion ===
					
					// perturbation level from 0.0 to 0.75 step: 0.01
					// 0.75 the maximum level that the char is still recognized by human
					$baseLevel = 0;					
					$step = 0.01;
					$level = $baseLevel;
					$maxLevel = 0.75;

					unset($settings);
					$settings['width'] 	= $WIDTH;
					$settings['height'] 	= $HEIGHT;
					$settings['length'] 	= $LENGTH; 
					$settings['char'] 	=  $char;
					while($level <= ($maxLevel + $step)) 
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings['fname'] = $fname;
						echo "<br/>" . $fname;						

						// distortion level
						$settings['level'] = $level;						

						// generate captcha						
						MySecurimage::generateCaptcha($settings);
						
						$level = $level + $step;
						$idx = $idx + 1;
					}

					// === END generate examples with distortion ===

					
					// === BEGIN plain examples with random rotation ===
					$numExamples = 10;
					unset($settings2);
					$settings2['minAngle'] 	= 0;
					$settings2['maxAngle'] 	= 360;
					$settings2['width'] 	= $WIDTH;
					$settings2['height'] 	= $HEIGHT;
					$settings2['length'] 	= $LENGTH; 
					$settings2['char'] 	=  $char;
					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings2['fname'] = $fname;
						echo "<br/>" . $fname;	
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings2);

						$idx = $idx + 1;
					}

					// === END plain examples with random rotation ===
				
					
					// === BEGIN examples with randomness ===

					// 10 examples with randomness:
					// - text color: black
					// - distortion: random distortion level
					// - lines: random number of lines (3 to 10)
					//          black
					$numExamples = 10;
					unset($settings3);
					$settings3['width'] 	= $WIDTH;
					$settings3['height'] 	= $HEIGHT;
					$settings3['length'] 	= $LENGTH; 
					$settings3['char'] 	=  $char;

					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename						
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings3['fname'] = $fname;
						echo "<br/>" . $fname;	

						$settings3['level'] = random_float(0.0,0.75);
						$settings3['lines'] = rand(3,10);
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings3);

						$idx = $idx + 1;
					}

					// === END examples with randomness ===
				}

				// === END generate digit training examples ===
				
				
				// === BEGIN generate lower case training letters ===
				
				$trainingLetters = "abcdefghijklmnopqrstuvwxyz";
				
				$numChars = strlen($trainingLetters);
				for($x=0; $x<$numChars; $x++)	
				{
					$idx = 0;
					
					$char = $trainingLetters{$x};
					
					$dirName = $char . '_';
					
					// create dir to store training examples
					if(!file_exists($DATA_DIR . '/' . $dirName))
					{
						mkdir($DATA_DIR . '/' . $dirName);
					}

					// create dir to store test data
					if(!file_exists($DATA_DIR_TEST . '/' . $dirName))
					{
						mkdir($DATA_DIR_TEST . '/' . $dirName);
					}


					// === BEGIN generate examples with distortion ===
					
					// perturbation level from 0.0 to 0.75 step: 0.01
					// 0.75 the maximum level that the char is still recognized by human
					$baseLevel = 0;					
					$step = 0.01;
					$level = $baseLevel;
					$maxLevel = 0.75;
					
					unset($settings);
					$settings['width'] 	= $WIDTH;
					$settings['height'] 	= $HEIGHT;
					$settings['length'] 	= $LENGTH; 
					$settings['char'] 	=  $char;
					while($level <= ($maxLevel + $step)) 
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings['fname'] = $fname;
						echo "<br/>" . $fname;	

						// distortion level
						$settings['level'] = $level;						

						// generate captcha						
						MySecurimage::generateCaptcha($settings);
						
						$level = $level + $step;
						$idx = $idx + 1;
					}

					// === END generate examples with distortion ===

					
					// === BEGIN plain examples with random rotation ===
					
					$numExamples = 10;
					
					unset($settings2);
					$settings2['minAngle'] 	= 0;
					$settings2['maxAngle'] 	= 360;
					$settings2['width'] 	= $WIDTH;
					$settings2['height'] 	= $HEIGHT;
					$settings2['length'] 	= $LENGTH; 
					$settings2['char'] 	=  $char;
					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings2['fname'] = $fname;
						echo "<br/>" . $fname;	
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings2);

						$idx = $idx + 1;
					}

					// === END plain examples with random rotation ===
				
					
					// === BEGIN examples with randomness ===

					// 10 examples with randomness:
					// - text color: black
					// - distortion: random distortion level
					// - lines: random number of lines (3 to 10)
					//          black
					$numExamples = 10;
					unset($settings3);
					$settings3['width'] 	= $WIDTH;
					$settings3['height'] 	= $HEIGHT;
					$settings3['length'] 	= $LENGTH; 
					$settings3['char'] 	=  $char;

					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings3['fname'] = $fname;
						echo "<br/>" . $fname;	

						$settings3['level'] = random_float(0.0,0.75);
						$settings3['lines'] = rand(3,10);
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings3);

						$idx = $idx + 1;
					}

					// === END examples with randomness ===
				}

				// === END generate lower case training letters ===
				
				
				
				// === BEGIN generate uppper case training letters ===
				
				$trainingLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
				
				$numChars = strlen($trainingLetters);
				for($x=0; $x<$numChars; $x++)	
				{
					$idx = 0;
					
					$char = $trainingLetters{$x};
					
					$dirName = strtoupper($char);
					
					// create dir to store training examples
					if(!file_exists($DATA_DIR . '/' . $dirName))
					{
						mkdir($DATA_DIR . '/' . $dirName);
					}

					// create dir to store test data
					if(!file_exists($DATA_DIR_TEST . '/' . $dirName))
					{
						mkdir($DATA_DIR_TEST . '/' . $dirName);
					}


					// === BEGIN generate examples with distortion ===
					
					// perturbation level from 0.0 to 0.75 step: 0.01
					// 0.75 the maximum level that the char is still recognized by human
					$baseLevel = 0;					
					$step = 0.01;
					$level = $baseLevel;
					$maxLevel = 0.75;

		
					unset($settings);
					$settings['width'] 	= $WIDTH;
					$settings['height'] 	= $HEIGHT;
					$settings['length'] 	= $LENGTH; 
					$settings['char'] 	=  $char;
					while($level <= ($maxLevel + $step)) 
					{
						// image filename						
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings['fname'] = $fname;
						echo "<br/>" . $fname;	

						// distortion level
						$settings['level'] = $level;						

						// generate captcha						
						MySecurimage::generateCaptcha($settings);
						
						$level = $level + $step;
						$idx = $idx + 1;
					}

					// === END generate examples with distortion ===

					
					// === BEGIN plain examples with random rotation ===
					
					$numExamples = 10;
					unset($settings2);
					$settings2['minAngle'] 	= 0;
					$settings2['maxAngle'] 	= 360;
					$settings2['width'] 	= $WIDTH;
					$settings2['height'] 	= $HEIGHT;
					$settings2['length'] 	= $LENGTH; 
					$settings2['char'] 	=  $char;
					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename						
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings2['fname'] = $fname;
						echo "<br/>" . $fname;	
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings2);

						$idx = $idx + 1;
					}

					// === END plain examples with random rotation ===
				
					
					// === BEGIN examples with randomness ===

					// 10 examples with randomness:
					// - text color: black
					// - distortion: random distortion level
					// - lines: random number of lines (3 to 10)
					//          black
					$numExamples = 10;
					unset($settings3);
					$settings3['width'] 	= $WIDTH;
					$settings3['height'] 	= $HEIGHT;
					$settings3['length'] 	= $LENGTH; 
					$settings3['char'] 	=  $char;

					for($i=1; $i<=$numExamples; $i++)
					{
						// image filename						
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $DATA_DIR_TEST . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $DATA_DIR . '/' . $dirName . '/' . $idx . ".gif";
						}

						$settings3['fname'] = $fname;
						echo "<br/>" . $fname;	

						$settings3['level'] = random_float(0.0,0.75);
						$settings3['lines'] = rand(3,10);
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings3);

						$idx = $idx + 1;
					}

					// === END examples with randomness ===
				}
				
				// === END generate uppper case training letters ===

				// === BEGIN generate rotation set ===
				// digit 0..9
				// 1 - 37: angle rotation 0 to 90
				// 38 - 74: angle rotation 270 to 360

				$trainingLetters = "0123456789";
				//$trainingLetters = "1";
				$numChars = strlen($trainingLetters);

				$trainingFolder = dirname(__FILE__) . '/rotation_training_data';
				$testFolder = dirname(__FILE__) . '/rotation_test_data';
				// create dir to store training examples
				if(!file_exists($trainingFolder))
				{
					mkdir($trainingFolder);
					
				}

				// create dir to store test data
				if(!file_exists($testFolder))
				{
					mkdir($testFolder);
				}


				// captcha settings
				unset($settings4);
				$settings4['width'] 	= $WIDTH;
				$settings4['height'] 	= $HEIGHT;
				$settings4['length'] 	= $LENGTH; 
				
				for($x=0; $x<$numChars; $x++)	
				{
					$char = $trainingLetters{$x};					
					$settings4['char'] 	=  $char;
					
					// first image will have 0 as the name
					$idx = 0;

					// directory where we store image
					$dirName = $char;
			
					// create dir to store training examples
					if(!file_exists($trainingFolder . '/' . $dirName))
					{
						mkdir($trainingFolder . '/' . $dirName);
						
					}

					// create dir to store test data
					if(!file_exists($testFolder . '/' . $dirName))
					{
						mkdir($testFolder . '/' . $dirName);
					}


					// generate 1 - 37 angle rotation 0 to 90;
					// in phpcaptcha text is read left-to-right
					// angle is the distance we raise the text from the base line
					// therefore 0 to 90 degree means 270 to 360 in phpcaptcha
					$settings4['minAngle'] 	= 270;
					for($i=1; $i<=37; $i++)
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $testFolder . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $trainingFolder . '/' . $dirName . '/' . $idx . ".gif";
						}		

						echo "<br/>" . $fname;
						$settings4['fname'] = $fname;				
						
						// set random maxAngle
						$settings4['maxAngle'] 	= mt_rand(270,360);					
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings4);

						// increase counter for file name
						$idx++;
					}


					// generate 1 - 37 angle rotation 270 to 360;
					// in phpcaptcha text is read left-to-right
					// angle is the distance we raise the text from the base line
					// therefore 270 to 360 degree means 0 to 90 in phpcaptcha
					$settings4['minAngle'] 	= 0;
					for($i=1; $i<=37; $i++)
					{
						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $testFolder . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $trainingFolder . '/' . $dirName . '/' . $idx . ".gif";
						}		

						echo "<br/>" . $fname;
						$settings4['fname'] = $fname;			
						
						// set random maxAngle
						$settings4['maxAngle'] 	= mt_rand(0,90);					
						
						// generate captcha						
						MySecurimage::generateCaptcha($settings4);

						// increase counter for file name
						$idx++;
					}


					
					
				}
				// === END generate rotation set ===
				
				// === BEGIN generate random lines set ===
				// digit 0..9
				// plain char
				// random lines 3 to 10 lines

				$trainingLetters = "0123456789";
				//$trainingLetters = "1";
				$numChars = strlen($trainingLetters);

				$trainingFolder = dirname(__FILE__) . '/lines_training_data';
				$testFolder = dirname(__FILE__) . '/lines_test_data';
				// create dir to store training examples
				if(!file_exists($trainingFolder))
				{
					mkdir($trainingFolder);
					
				}

				// create dir to store test data
				if(!file_exists($testFolder))
				{
					mkdir($testFolder);
				}

				// captcha settings
				unset($settings5);
				$settings5['width'] 	= $WIDTH;
				$settings5['height'] 	= $HEIGHT;
				$settings5['length'] 	= $LENGTH; 
				
				for($x=0; $x<$numChars; $x++)	
				{
					$char = $trainingLetters{$x};					
					$settings5['char'] 	=  $char;
					
					// first image will have 0 as the name
					$idx = 0;

					// directory where we store image
					$dirName = $char;

					// create dir to store training examples
					if(!file_exists($trainingFolder . '/' . $dirName))
					{
						mkdir($trainingFolder . '/' . $dirName);
						
					}

					// create dir to store test data
					if(!file_exists($testFolder . '/' . $dirName))
					{
						mkdir($testFolder . '/' . $dirName);
					}


					for($i=1; $i<=74; $i++)
					{
						// lines randomly between 3 to 10
						$settings5['lines'] = mt_rand(3,10);	

						// image filename 
						// 70% for training_data
						// 30% for test_data
						if((($idx+1) % $TRAINING_SIZE) == 0)
						{
							$fname = $testFolder . '/' . $dirName . '/' . $idx . ".gif";
						}
						else
						{ 						
							$fname = $trainingFolder . '/' . $dirName . '/' . $idx . ".gif";
						}		

						echo "<br/>" . $fname;	
						$settings5['fname'] = $fname;							
											
						// generate captcha						
						MySecurimage::generateCaptcha($settings5);

						// increase counter for file name
						$idx++;
					}	
					
				}
				// === END generate random lines set ===

			
			?>		
		</p>	
	</body>
</html>
